/**
 *
 * @author 
 *
 */
class BuildManage {
    private enterFrameCount: number = 0;
    
    public MyZhuBuildList: BuildBase[] = [];
    public MyFuBuildList: BuildBase[] = [];
	
    private static _i: BuildManage;
    public static get i(): BuildManage {
        if(!this._i)
            this._i = new BuildManage();
        return this._i;
    }
	public constructor() {
	}
		
    private objCon: egret.DisplayObjectContainer;
    public setup(obj: egret.DisplayObjectContainer): void {
        this.objCon = obj;
        this.initView();
    }
    public enterFrame(): void {
        this.control();
    }
	
    public GameReStart(): void {
		this.enterFrameCount = 0;
		this.objCon.removeChildren();
		this.MyZhuBuildList = [];
		this.MyFuBuildList = [];
        this.initView();
    }
    
    private initView(): void {
        var build1: BuildBase = new BuildBase();
        build1.data = new BuildCityInfo();
        build1.x = 192;
        build1.y = 544;
        this.objCon.addChild(build1);
        this.MyZhuBuildList.push(build1);
        
        var build2: BuildBase = new BuildBase();
        build2.data = new BuildTowerInfo();
        build2.x = 320;
        build2.y = 544;
        this.objCon.addChild(build2);
        this.MyFuBuildList.push(build2);
    }
    private control(): void {
		var MyZhuBuildDie: number = 0;
			
        for(var j in this.MyFuBuildList) {
            var buildTemp = this.MyFuBuildList[j];
			if(this.enterFrameCount % 10 == 0) {
				if(buildTemp.data.bloodVolume==0){
					MyZhuBuildDie++;
				}
				if(MyZhuBuildDie == this.MyFuBuildList.length){
					Global.dispatchEvent(MainNotify.openGameOverPanelNotify);
					return;
				}
			}
			
            //目标敌人
            if(EnemyManage.i.armyList.length == 0) break;
            var enemyTemp;
            for(var i in EnemyManage.i.armyList) {
                enemyTemp = EnemyManage.i.armyList[0];
                if(EnemyManage.i.armyList[i].y > enemyTemp.y) {
                    enemyTemp = EnemyManage.i.armyList[i];
                }
            }
            //接近敌人发出子弹
            if(CommonFunction.GetDistance(new egret.Point(buildTemp.x + 32,buildTemp.y + 32),new egret.Point(enemyTemp.x + 24,enemyTemp.y + 32)) < buildTemp.data.hitDistance) {
                //判断时间间隔
                if(this.enterFrameCount - buildTemp.data.prehitTime > buildTemp.data.hitDelayTime || buildTemp.data.prehitTime == 0) {
                    buildTemp.data.prehitTime = this.enterFrameCount;
                    BowManage.i.create(buildTemp,enemyTemp);
                }
            }

        }


        this.enterFrameCount++;
    }
}
